
<html>
    
       <head>
                 <title>Truong Cong Thuc</title>
                 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                 <link rel="stylesheet" href="public/css/style_ontap.css" type="text/css">
                 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
        </head>
    <body>
     

<?php
require_once './mode/database.php';
?>


<?php
$query = "SELECT * FROM companies";
$companies = getData($query);
$querya = "SELECT * FROM apps";
$apps = getData($querya);
?>

<div id="form">
    <p class="tieude"><h1>Truong Cong Thuc</h1></p>
<form method="POST">
        <table border="20">
            <tr><td> Min fans</td><td><input type="text" name="min_fans" id="min_fans"></td></tr></br>
            <tr><td> Max fans</td><td><input type="text" name="max_fans" id="max_fans"></td></tr></br>
            <tr><td> Keyword</td><td> <input type="text" name="keyword"></td></tr></br>
            <tr><td> Company</td><td><select name="id_company">
                <option value="0"><?php echo "   Company  " ?></option>
                <?php foreach ($companies as $company): ?>

                    <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
                <?php endforeach; ?>
            </select></td></tr></br>
            <tr><td> Apps</td><td><select name="id_apps">
                <option value="0"><?php echo "   App  " ?></option>
                <?php foreach ($apps as $app): ?>

                    <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
                <?php endforeach; ?>
            </select></td></tr></br>
            
        </table>
    <input type="submit" name="submit" value="Search" >
    </form>
</div>
<?php if (isset($_POST['submit'])): ?>
    <?php
  $min_fans = $_POST['min_fans']; //TODO
    $max_fans = $_POST['max_fans']; //TODO
    $keyword = $_POST['keyword']; //TODO
    if (empty($max_fans) && !empty($min_fans)) {
        $query = "SELECT * FROM pages 
                  
                 WHERE
                     (likes >= $min_fans)
                     AND (description LIKE '% $keyword %')
                  LIMIT 0, 15
                 ";
   } else if (!empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages 
                 
                  WHERE
                      (likes <= $max_fans)
                     AND (description LIKE '% $keyword %')
                      
                 LIMIT 0, 15
                  ";
   } else if (empty($max_fans) && empty($min_fans)) {
       $query = "SELECT * FROM pages
            WHERE (description LIKE '% $keyword %')
                  LIMIT 0, 15
                  ";
    } else {
        $query = "SELECT * FROM pages    
                  WHERE
                     (likes >= $min_fans)
                      AND (likes <= $max_fans)
                      AND (description LIKE '% $keyword %')
                  LIMIT 0, 15
                  ";
    }
  
    $pages = getData($query);
    ?>
    
    
    <?php 
   
        $min_fans = $_POST['min_fans'];//TODO
        $max_fans = $_POST['max_fans'];//TODO
        $keyword = $_POST['keyword'];//TODO
        $company=$_POST["id_company"];
        $apps=$_POST["id_apps"];
        if(!empty($company))
            $company="(companies.id=$company) AND";
        else
           $company="";
        if(!empty($apps))
            $apps="(apps.id='$apps') AND";
        else
           $apps="";
        if(empty($max_fans)&& !empty($min_fans))
        {
          $query="SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                 ( pages.likes>= $min_fans) and
                  (pages.description LIKE '% $keyword %')
                  LIMIT 0,10";
        }
        else if(!empty($max_fans)&& empty($min_fans))
        {
          $query="SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                 ( pages.likes<= $max_fans) and
                 ( pages.description LIKE '% $keyword %')
                  LIMIT 0,10";
        }
        else if(empty($max_fans)&& empty($min_fans))
        {
           $query="SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                 ( pages.description LIKE '% $keyword %')
                  LIMIT 0,10";
        }
        else
        {  
        $query="SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                 ( pages.likes>= $min_fans) and
                  (pages.likes<= $max_fans) and
                 ( pages.description LIKE '% $keyword %')
                  LIMIT 0,10";
        }
        var_dump($query);
        $pages = getData($query);     
    ?>
    
    
    
<?php else: ?> 
    <?php
    $query = "SELECT * FROM pages LIMIT 0,15";
    $pages = getData($query);
    ?>
<?php endif; ?>
<?php if (!empty($pages)): ?>

    <table >
        <tr >
              <th>ID</th>
                <th>Picture</th>
                <th>Pid</th>
                <th>Name</th>
                <th>Username</th>
                <th>Phone</th>
                <th>Like</th>
                <th>link</th>
               
               
               <td> Company </td>
            <td> description </td>
        </tr>
        <?php foreach ($pages as $page): ?>
            <tr>
                <td><?php echo $page['id'] ?></td>
                <td><img src='<?php echo $page['picture'] ?>'></td>
                 <td><?php echo $page['pid'] ?></td>
                  <td><?php echo $page['name'] ?></td>
                 <td><?php echo substr($page['description'], 0, 100) ?></td> 
                    <td><?php echo $page['username'] ?></td>
                    <td><?php echo $page['phone'] ?></td>
                    <td><?php echo $page['likes'] ?></td> 
                  
                   
                    
               
            </tr>
        <?php endforeach; ?>
    </table>
<?php endif; ?>
    

 <script>
    $(document).ready(function (){
        $("#min_fans").click(function (){
           var min_fans= $("#min_fans").val();
          if ($.isNumeric(min_fans)){
              alert ('is number');              
          }else {
           alert ('is not number');   
              
          }
        });
        
    });
    
</script>
 <script>
    $(document).ready(function (){
        $("#max_fans").click(function (){
           var max_fans= $("#max_fans").val();
          if ($.isNumeric(max_fans)){
              alert ('is number');              
          }else {
           alert ('is not number');   
              
          }
        });
        
    });
    
</script>
   </body>
</html>